// ==============================================================================================
// RibbonControl.cs
//
// Created by: NI, 2007.03.27
// Revised by: NI, 2007.11.23
// ==============================================================================================
using System.Collections.Generic;
using RibbonExplorer.Entities;
using RibbonExplorer.Storage;

namespace RibbonExplorer.Entities
{
  // ==============================================================================================
  /// <summary>
  /// This class represents a control on a group.
  /// </summary>
  // ==============================================================================================
  public class RibbonControl : ItemBase
  {
    #region Private fields

    private Group _ParentGroup;
    private RibbonTableItem _Properties;
    private Dictionary<string, RibbonControl> _Controls = new Dictionary<string, RibbonControl>();

    #endregion

    #region Lifecycle methods

    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Creates a ribbon control instance from the specified ID and name.
    /// </summary>
    /// <param name="id">Tab ID</param>
    /// <param name="controlItem">Ribbon table item belonging to this control.</param>
    /// <param name="parent">Parent ribbon tab</param>
    // --------------------------------------------------------------------------------------------
    public RibbonControl(string id, RibbonTableItem controlItem, Group parent)
      : base(id)
    {
      _ParentGroup = parent;
      _Properties = controlItem;
      LoadChildren();
    }

    #endregion

    #region Public properties

    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Gets the parent group of this ribbon control.
    /// </summary>
    // --------------------------------------------------------------------------------------------
    public Group ParentGroup
    {
      get { return _ParentGroup; }
    }

    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Gets the proerties of the ribbon table control.
    /// </summary>
    // --------------------------------------------------------------------------------------------
    public RibbonTableItem Properties
    {
      get { return _Properties; }
    } 
    
    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Gets the child controls owned by this control
    /// </summary>
    // --------------------------------------------------------------------------------------------
    public Dictionary<string, RibbonControl> Controls
    {
      get { return _Controls; }
    }

    #endregion

    #region Overridden methods

    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Loads the child controls belonging to this control.
    /// </summary>
    // --------------------------------------------------------------------------------------------
    protected override void OnLoadChildren()
    {
      // --- Filter only menu, split buttons and gallerys
      ControlType thisType = Properties.Type;
      if (thisType != ControlType.Menu &&
          thisType != ControlType.SplitButton &&
          thisType != ControlType.Gallery) return;

      // --- Try to obtain the children
      RibbonTable ribbonTable;
      if (RibbonTableCache.TryGetValue(_ParentGroup.ParentRibbon.Id, out ribbonTable))
      {
        // --- Obtain child controls
        List<RibbonTableItem> children = _ParentGroup.FilterChildren(ribbonTable, Id);
        foreach (RibbonTableItem item in children)
        {
          RibbonControl newControl = new RibbonControl(item.ControlId, item, _ParentGroup);
          {
            if (!_Controls.ContainsKey(newControl.Id))
            {
              _Controls.Add(newControl.Id, newControl);

              // --- Obtain grand children
              List<RibbonTableItem> grandChildren = 
                _ParentGroup.FilterGrandChildren(ribbonTable, newControl.Id, Id);
              foreach (RibbonTableItem grandItem in grandChildren)
              {
                RibbonControl newGrandControl = 
                  new RibbonControl(grandItem.ControlId, item, _ParentGroup);
                {
                  if (!newControl.Controls.ContainsKey(newGrandControl.Id))
                  {
                    newControl.Controls.Add(newGrandControl.Id, newGrandControl);
                  }
                }
              }
            }
          }
        }
      }
    }

    #endregion
  }
}